<?php
namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\AbstractExporter;

class BaomingExporter extends AbstractExporter
{
    public function export()
    {
        $filename = $this->getTable() . '.csv';

        // 这里获取数据
//        dd($this->getData());

        $list = $this->getData();

        $output = '';



        if (!empty($list)) {

            //标题
            $titleArr = explode('<br/>', $list[0]['content']);

            $arr = [];
            foreach ($titleArr as $vv) {
                $vv = explode(':', $vv);
                $strV = '';
                if (isset($vv[0])) {
                    $strV = $vv[0];
                }
                array_push($arr, $strV);
            }
            $output .= implode(',', $arr) . "\n";



            //数据
            foreach ($list as $k => $v) {
                $content = explode('<br/>', $v['content']);

                $arr = [];
                foreach ($content as $vv) {
                    $vv = explode(':', $vv);

                    $strV = '';
                    if (isset($vv[1])) {
                        $strV = $vv[1];
                    }
                    array_push($arr, $strV);
                }

                $output .= implode(',', $arr) . "\n";
            }

//            dd($output);
        }


        // 在这里控制你想输出的格式,或者使用第三方库导出Excel文件
        $headers = [
            'Content-Encoding' => 'UTF-8',
            'Content-Type' => 'text/csv;charset=UTF-8',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];

        $output = iconv('UTF-8', 'GBK//IGNORE', isset($output)? $output:'');

//        $output = mb_convert_encoding($output, "UTF-8", "GBK");

        // 导出文件，
        response(rtrim($output, "\n"), 200, $headers)->send();

        exit;
    }
}